from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
import cv2

model_id = 'damo/cv_tinynas_human-detection_damoyolo'
input_location = 'P1.jpg'

human_detection = pipeline(Tasks.domain_specific_object_detection, model=model_id)
result = human_detection(input_location)
print("result is : ", result)

# 加载图片
image = cv2.imread(input_location)

# 检查是否成功加载图片
if image is None:
    print("Error: Unable to load image. Please check the file path.")
else:
    # 遍历检测结果
    for detection in result['boxes']:
        # 提取坐标信息
        x1=detection[0]
        y1=detection[1]
        x2=detection[2]
        y2=detection[3]
        
        x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)  # 转换为整数

        # 绘制矩形框
        cv2.rectangle(image, (x1, y1), (x2,y2), (0, 255, 0), 2)  # 绿色框

        # 添加标签（可选）
        label = 'Person'
        cv2.putText(image, label, (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

    # 显示图片
    cv2.imshow('Detected Image', image)
    cv2.waitKey(0)  # 等待用户按键
    cv2.destroyAllWindows()  # 关闭窗口